package com.xiaoq.shop.repository.impl;

import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.entity.SPUEntity;
import com.xiaoq.shop.entity.SPUReviewEntity;
import com.xiaoq.shop.repository.ISPURepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.DeleteOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/11/28.
 */
@Repository
public class SPURepositoryImpl implements ISPURepository {

    @Autowired
    private Datastore datastore;

    @Override
    public boolean addReview2Spu(String shopId, String spuCode, SPUReviewEntity spuReviewEntity) {
        if(StringUtils.isAnyBlank(shopId, spuCode) || spuReviewEntity==null){
            return false;
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class);

        spuEntityQuery
                .filter("shopId", shopId)
                .filter("spuCode", spuCode);
        UpdateOperations<SPUEntity> updateOperations = datastore.
                createUpdateOperations(SPUEntity.class)
                .addToSet("spuReviews", spuReviewEntity);
        datastore.update(spuEntityQuery, updateOperations);
        return true;
    }

    @Override
    public boolean save(SPUEntity spuEntity) {
        if(spuEntity==null || StringUtils.isBlank(spuEntity.getId())){
            return false;
        }
        datastore.save(spuEntity);
        return true;
    }

    @Override
    public SPUEntity getSPUEntityByCommon(SKUEntity skuEntity) {
        if(skuEntity == null){
            return null;
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class)
                .filter("saleState", 1);;

        if(StringUtils.isNotBlank(skuEntity.getShopId())){
            spuEntityQuery.filter("shopId", skuEntity.getShopId());
        }
        if(StringUtils.isNotBlank(skuEntity.getSpuName())){
            spuEntityQuery.filter("spuName", skuEntity.getSpuName());
        }
        if(StringUtils.isNotBlank(skuEntity.getSpuCode())){
            spuEntityQuery.filter("spuCode", skuEntity.getSpuCode());
        }

        return spuEntityQuery.get();
    }

    @Override
    public boolean updateById(SPUEntity spuEntity) {
        if(spuEntity==null || StringUtils.isBlank(spuEntity.getId())){
            return false;
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class)
                .filter("_id", spuEntity.getId());
        UpdateOperations<SPUEntity> ops = datastore.createUpdateOperations(SPUEntity.class);
        if(StringUtils.isNotBlank(spuEntity.getSpuName())){
            ops.set("spuName", spuEntity.getSpuName());
        }
        if(StringUtils.isNotBlank(spuEntity.getCatalogName())){
            ops.set("catalogName", spuEntity.getCatalogName());
        }
        if(StringUtils.isNotBlank(spuEntity.getSpuCode())){
            ops.set("spuCode", spuEntity.getSpuCode());
        }
        if(StringUtils.isNotBlank(spuEntity.getSpuCatalogCode())){
            ops.set("spuCatalogCode", spuEntity.getSpuCatalogCode());
        }
        if(StringUtils.isNotBlank(spuEntity.getSpuCatalogName())){
            ops.set("spuCatalogName", spuEntity.getSpuCatalogName());
        }
        if(StringUtils.isNotBlank(spuEntity.getBrandCode())){
            ops.set("brandCode", spuEntity.getBrandCode());
        }
        if(StringUtils.isNotBlank(spuEntity.getBrandName())){
            ops.set("brandName", spuEntity.getBrandName());
        }
        if(StringUtils.isNotBlank(spuEntity.getDescription())){
            ops.set("description", spuEntity.getDescription());
        }

        datastore.update(spuEntityQuery, ops);
        return true;
    }

    @Override
    public boolean deleteByShopIdAndSpuCode(String shopId, String spuCode) {
        if(StringUtils.isAnyBlank(shopId, spuCode)){
            return false;
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class)
                .filter("shopId", shopId)
                .filter("spuCode", spuCode);
        DeleteOptions deleteOptions = new DeleteOptions();
        deleteOptions.writeConcern(WriteConcern.ACKNOWLEDGED);
        WriteResult writeResult = datastore.delete(spuEntityQuery, deleteOptions);
        return writeResult.getN()==1;
    }

    @Override
    public List<SPUEntity> getSPUByShopIdAndCatalogCode(String shopId, String catalogCode) {
        if(StringUtils.isBlank(shopId)){
            return new ArrayList<>();
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class)
                .filter("shopId", shopId);
        if(StringUtils.isNotBlank(catalogCode)){
            spuEntityQuery.filter("catalogCode", catalogCode);
        }

        return spuEntityQuery.asList();
    }

    @Override
    public boolean deleteByShopIdAndSpucodeList(String shopId, List<String> spuCodeList) {
        if(StringUtils.isBlank(shopId) || CollectionUtils.isEmpty(spuCodeList)){
            return false;
        }
        Query<SPUEntity> spuEntityQuery = datastore.createQuery(SPUEntity.class)
                .filter("shopId", shopId)
                .filter("spuCode in", spuCodeList);
        DeleteOptions deleteOptions = new DeleteOptions();
        deleteOptions.writeConcern(WriteConcern.ACKNOWLEDGED);
        WriteResult writeResult = datastore.delete(spuEntityQuery, deleteOptions);
        return writeResult.getN()>0;
    }
}
